home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / fileutil.zip / POSIXTIM.Y < prev    next >
Text File  |  1992-02-22  |  4KB  |  178 lines

  1. /* Parse dates for touch.
  2.    Copyright (C) 1987, 1989, 1990, Free Software Foundation Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by Jim Kingdon and David MacKenzie. */
  19.  
  20. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  21.    This port is also distributed under the terms of the
  22.    GNU General Public License as published by the
  23.    Free Software Foundation.
  24.  
  25.    Please note that this file is not identical to the
  26.    original GNU release, you should have received this
  27.    code as patch to the official release.  */
  28.  
  29. %token digit
  30.  
  31. %{
  32. #ifdef MSDOS
  33. static char RCS_Id[] =
  34.   "$Header: e:/gnu/fileutil/RCS/posixtim.y 1.4.0.2 90/09/19 12:54:32 tho Exp $";
  35. #endif /* MSDOS */
  36.  
  37. #include <stdio.h>
  38. #include <sys/types.h>
  39. #include <time.h>
  40.  
  41. /* Used by bison.simple in old (1.08 and older) versions of bison.  */
  42. #ifdef __GNUC__
  43. #define alloca __builtin_alloca
  44. #else
  45. #ifdef sparc
  46. #include <alloca.h>
  47. #endif
  48. #endif
  49.  
  50. #define YYDEBUG 1
  51.  
  52. static char *curpos;
  53.  
  54. static struct tm t;
  55.  
  56. time_t mktime ();
  57.  
  58. #ifdef MSDOS
  59. #include <malloc.h>
  60. static int yyerror (void);
  61. static int yylex (void);
  62. #else /* not MSDOS */
  63. static int yylex ();
  64. static int yyerror ();
  65. #endif /* not MSDOS */
  66.  
  67. #define yyparse posixtime_yyparse
  68. %}
  69.  
  70. %%
  71. date :
  72.        digitpair /* month */
  73.        digitpair /* day */
  74.        digitpair /* hours */
  75.        digitpair /* minutes */
  76.        year
  77.        seconds {
  78.              if ($1 >= 1 && $1 <= 12)
  79.            t.tm_mon = $1 - 1;
  80.          else {
  81.            YYABORT;
  82.          }
  83.          if ($2 >= 1 && $2 <= 31)
  84.            t.tm_mday = $2;
  85.          else {
  86.            YYABORT;
  87.          }
  88.          if ($3 >= 0 && $3 <= 23)
  89.            t.tm_hour = $3;
  90.          else {
  91.            YYABORT;
  92.          }
  93.          if ($4 >= 0 && $4 <= 59)
  94.            t.tm_min = $4;
  95.          else {
  96.            YYABORT;
  97.          }
  98.            }
  99.  
  100. year : digitpair {
  101.                    t.tm_year = $1;
  102.            /* Deduce the century based on the year.
  103.               See POSIX.2 section 4.63.3.  */
  104.            if ($1 <= 68)
  105.              t.tm_year += 100;
  106.          }
  107.     | digitpair digitpair {
  108.                             t.tm_year = $1 * 100 + $2;
  109.                 if (t.tm_year < 1900) {
  110.                   YYABORT;
  111.                 } else
  112.                   t.tm_year -= 1900;
  113.               }
  114.     | /* empty */ {
  115.                     time_t now;
  116.             struct tm *tmp;
  117.  
  118.                     /* Use current year.  */
  119.                     time (&now);
  120.             tmp = localtime (&now);
  121.             t.tm_year = tmp->tm_year;
  122.           }
  123.     ;
  124.  
  125. seconds : /* empty */ {
  126.                         t.tm_sec = 0;
  127.               }
  128.         | '.' digitpair {
  129.                       if ($2 >= 0 && $2 <= 61)
  130.                 t.tm_sec = $2;
  131.               else {
  132.                 YYABORT;
  133.               }
  134.             }
  135.         ;
  136.  
  137. digitpair : digit digit {
  138.                           $$ = $1 * 10 + $2;
  139.             }
  140.           ;
  141. %%
  142. static int
  143. yylex ()
  144. {
  145.   char ch = *curpos++;
  146.  
  147.   if (ch >= '0' && ch <= '9')
  148.     {
  149.       yylval = ch - '0';
  150.       return digit;
  151.     }
  152.   else if (ch == '.' || ch == '\0')
  153.     return ch;
  154.   else
  155.     /* Cause an error.  */
  156.     return '?';
  157. }
  158.  
  159. static int
  160. yyerror ()
  161. {
  162.   return 0;
  163. }
  164.  
  165. /* Parse a POSIX-style date and return it, or (time_t)-1 for an error.  */
  166. time_t
  167. posixtime (s)
  168.      char *s;
  169. {
  170.   curpos = s;
  171.   /* Let mktime() decide whether it is daylight savings time.  */
  172.   t.tm_isdst = -1;
  173.   if (yyparse ())
  174.     return (time_t)-1;
  175.   else
  176.     return mktime (&t);
  177. }
  178.